package com.xongxong.admin.web.config.loginCommon.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;
import com.google.common.cache.Cache;
import com.xongxong.admin.common.constants.Constants;
import com.xongxong.admin.common.response.Response;
import com.xongxong.admin.mapper.UserInfoMapper;
import com.xongxong.admin.utils.threadLocalUtils.UserIdUtils;
import com.xongxong.admin.web.config.loginCommon.annotation.CheckOutLogin;
import com.xongxong.domain.vo.userVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

//声明一个类为切面类
//将该类注入到IOC容器中，交给Spring管理
@Aspect
@Component
@Slf4j
public class AspectConfig {
    @Resource
    private Cache<String, userVo> userInfoCache;

    @Resource
    private UserInfoMapper userInfoMapperl;

    /**
     * 逻辑,请求头拿到token,校验是否过期,token一切正常放行,并且通过token从本地缓存中获取到对应的UserId,
     * 放到本地线程中,让查询可以全局获取用户信息。
     * todo 校验这一个用户有哪些角色,通过角色判断这一个用户有哪些权限-然后生成前端需要的菜单等
     * 如果没有token,就报错401没有权限,
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.xongxong.admin.web.config.loginCommon.annotation.CheckOutLogin)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            // 获取HttpServletRequest对象
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 获取请求头信息
            String token = request.getHeader(Constants.CachStatusEnum.EASY_ADMIN_TOKEN.getDesc());
            if (token==null){
            return Response.error("401","用户未登入",null);
            }
            userVo user = userInfoCache.getIfPresent(Constants.CachStatusEnum.EASY_ADMIN_TOKEN.getDesc());
            if (user != null) {
                UserIdUtils.set(user);
                return joinPoint.proceed();
            }
            int index = token.indexOf(':');
            assert  index!= -1;
            String userId = token.substring(index + 1);
            // 保证该id在数据库中存在
            userVo userVo=userInfoMapperl.getDataById(userId);
            assert userVo!=null;
            if (ObjectUtil.isNotEmpty(userVo.getId()) && ObjectUtil.isNotEmpty(userVo.getNickName())) {
                userVo.setToken(token);
                UserIdUtils.set(userVo);
                userInfoCache.put(Constants.CachStatusEnum.EASY_ADMIN_TOKEN.getDesc(), userVo);
            }
            return  joinPoint.proceed();
        }finally {
            UserIdUtils.remove();
        }

    }
}
